package controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import dto.LoginUserDTO;
import dto.UserInfoDTO;
import entity.User;
import service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2023-12-24 21:55:00
 */
@RestController
@Slf4j
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    @GetMapping("userInfo")
    public ResponseEntity<UserInfoDTO> queryUserInfo() {
        return ResponseEntity.ok(this.userService.findUserInfo(1));
    }
    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<User>> queryByPage(User user, PageRequest pageRequest) {
        return ResponseEntity.ok(this.userService.queryByPage(user, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<User> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<User> add(User user) {
        return ResponseEntity.ok(this.userService.insert(user));
    }


    /**
     * 注册用户
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping("register")
    public ResponseEntity<Integer> add(@RequestBody@Validated User user, BindingResult bindingResult) {
        return ResponseEntity.ok(this.userService.register(user));
    }

    /**
     * 登录
     *
     * @param user 传入的user对象
     * @param bindingResult 校验对象
     * @return 返回一个封装着登录用户相关信息的LoginUser对象
     * @throws JsonProcessingException json异常
     */
    @PostMapping("login")
    public ResponseEntity<Object> search(@RequestBody @Validated User user, BindingResult bindingResult) throws JsonProcessingException {
        System.out.println("请求发过来了");

        List<ObjectError> allErrors = bindingResult.getAllErrors();
        if(allErrors.size() > 0) {
            allErrors.forEach(error -> log.error("登录时用户校验失败：{}",error.getDefaultMessage()));
            return  new  ResponseEntity<>("用户名或密码不匹配", HttpStatus.ACCEPTED);
        }
        LoginUserDTO loginUser = null;
        try {
            loginUser = userService.selectUser(user);
        } catch (Exception e) {
            return  new  ResponseEntity<>(e.getMessage(), HttpStatus.ACCEPTED);
        }
        return ResponseEntity.ok().body(loginUser);
    }

    /**
     * 安全退出
     *
     * @return 返回退出成功信息
     */
    @GetMapping("logout")
    public ResponseEntity<String> logout(){
        try{
            userService.logout();
        }catch (Exception e){
            return ResponseEntity.status(500).build();
        }
        return ResponseEntity.ok().body("退出成功");
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<User> edit(User user) {
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.userService.deleteById(id));
    }
}